// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#ifndef __H_MK_UI_WINDOW__
#define __H_MK_UI_WINDOW__

#include "mk_ui_define.h"
#include "mk_ui_appearence.h"
#include "mk_ui_renderer.h"
#include "mk_ft_font.h"
#include "mk_ui_font_render.h"

namespace MK3DE
{
    class MK3DE_API CUIWindow
    {
    public:
        CUIWindow(CUIRenderer* renderer, CUIWindow* parent, D3DXVECTOR2& pos, D3DXVECTOR2& size, const char* name);
        virtual ~CUIWindow();

        CUIWindow* GetParent() { return _parent; }
        int GetId() { return _id; }
        D3DXVECTOR2 GetPosition() { return _pos; }
        void SetPosition(float x, float y);
        D3DXVECTOR2 GetSize() { return _size; }
        std::string GetName() { return _name; }
        std::wstring GetCaption() { return _fontRender.GetText(); }
        void SetCaption(const wchar_t* cap) { _fontRender.SetText(cap); }

        BOOL IsPointIn(D3DXVECTOR2* pt);
        CUIWindow* GetWindowAtPoint(D3DXVECTOR2* pt);

        virtual UI_CONTROL_TYPE GetType() { return UICT_WINDOW; }
        virtual BOOL Show(BOOL show);
        virtual BOOL MsgProc(UIInputEvent& event);
        virtual BOOL KeyboardMsgProc(UIInputEvent& event) { return FALSE; }
        virtual BOOL MouseMsgProc(UIInputEvent& event) { return FALSE; }
        virtual void OnMouseEnter() {}
        virtual void OnMouseLeave() {}
        virtual void OnFocusSet() {}
        virtual void OnFocusLost() {}
        
        virtual BOOL AddChild(CUIWindow* child);
        virtual BOOL RemoveChild(CUIWindow* child);

        CUIWindow* GetChild(int id);
        BOOL DeleteChildren();

        virtual BOOL Update(DWORD dt);
        virtual BOOL Render();

        virtual void MakeDefaultAppearence() {}

        CUIFontRender& GetFontRender() { return _fontRender; }

        void SetBorder(float border);

        static void SetMouseHover(CUIWindow* control);
        static CUIWindow* GetMouseHover() { return _s_mouseHover; }

        static void SetFocus(CUIWindow* focus);
        static CUIWindow* GetFocus() { return _s_focus; }

    protected:
        CUIRenderer* _renderer;
        CUIWindow* _parent;
        int _id;
        D3DXVECTOR2 _pos;
        D3DXVECTOR2 _size;
        std::string _name;

        std::vector<CUIWindow*> _children;

        BOOL _show;

        CUIFontRender _fontRender;

        CUIWindowAppearence _appearence;

        float _border;

        static DWORD UIDAC_PANEL_BG_NORMAL[4];
        static DWORD UIDAC_PANEL_BG_FOCUS[4];
        static DWORD UIDAC_PANEL_BG_HOVER[4];
        static DWORD UIDAC_BORDER_LIGHT[4];
        static DWORD UIDAC_BORDER_DARK[4];

    private:
        static CUIWindow* _s_mouseHover;
        static CUIWindow* _s_focus;

        static DWORD __id;

        DWORD __NewId()
        {
            return __id++;
        }
    };
};

#endif // __H_MK_UI_WINDOW__